/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.action.auth.login.flow;

import cn.hallele.infrastructure.metadata.exception.children.ActionException;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.infrastructure.plugin.captcha.CaptchaClient;
import cn.hallele.ms.portalcenter.application.bo.AuthBO;
import cn.hallele.ms.portalcenter.integration.common.constants.POCMsgCodeConst;
import cn.hallele.ms.starter.web.framework.node.ServiceFlow;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 校验 验证码
 *
 * @author anle5
 * @since 2024/12/31 22:54
 */
@Slf4j
@LiteflowComponent
public class VerifyCaptchaFlow extends ServiceFlow {
    @Autowired
    private CaptchaClient captchaClient;

    @Override
    public void process() throws Exception {
        AuthBO authBO = this.getRequestData();;
        String captchaCode = authBO.getCaptchaCode();
        if (ObjectUtil.isBlank(captchaCode)) {
            log.error("captcha code is blank.");
            throw new ActionException(POCMsgCodeConst.CAPTCHA_ERROR);
        }
        boolean verify = captchaClient.verify(captchaCode);
        if (!verify) {
            log.error("captcha code verify failed.");
            throw new ActionException(POCMsgCodeConst.CAPTCHA_ERROR);
        }

    }
}
